package com.example.companybusiness.operationt.room;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.companybusiness.MainActivity;

import java.util.List;

public class DBEngine {

    private  SearchBoxDao searchBoxDao;

    public DBEngine(Context context){
        SearchBoxDatabase searchBoxDatabase = SearchBoxDatabase.getInstance(context);
        searchBoxDao = searchBoxDatabase.getSearchBoxDao();
    }

    //Dao增删改查

    //insert 删除
    public void insertSearchBoxs(SearchBox...searchBoxes){
        new InsertAsyncTask(searchBoxDao).execute(searchBoxes);
    }
    //update 更新
    public void updateSearchBoxs(SearchBox...searchBoxes){
        new UpdateAsyncTask(searchBoxDao).execute(searchBoxes);
    }
    //delete 删除(条件删除)
    public void deleteSearchBoxs(SearchBox...searchBoxes){
        new DeleteAsyncTask(searchBoxDao).execute(searchBoxes);
    }
    //delete 全部删除
    public void deleteAllSearchBoxs(){
        new DeleteAllAsyncTask(searchBoxDao).execute();
    }
    //query 全部查询
    public void querySearchBoxs(){
        new QueryAsyncTask(searchBoxDao).execute();
    }




    //使用数据库，默认异步线程 ======== 异步操作
    //insert 插入
    static class InsertAsyncTask extends AsyncTask<SearchBox,Void,Void>{

        private SearchBoxDao dao;

        public InsertAsyncTask(SearchBoxDao searchBoxDao) {
            dao = searchBoxDao;
        }

        @Override
        protected Void doInBackground(SearchBox... searchBoxes) {
            dao.insertSearchBox(searchBoxes);
            return null;
        }
    }
    //update 更新
    static class UpdateAsyncTask extends AsyncTask<SearchBox,Void,Void>{

        private SearchBoxDao dao;

        public UpdateAsyncTask(SearchBoxDao searchBoxDao) {
            dao = searchBoxDao;
        }

        @Override
        protected Void doInBackground(SearchBox... searchBoxes) {
            dao.updateSearchBox(searchBoxes);
            return null;
        }
    }
    //delete 删除(条件删除)
    static class DeleteAsyncTask extends AsyncTask<SearchBox,Void,Void>{

        private SearchBoxDao dao;

        public DeleteAsyncTask(SearchBoxDao searchBoxDao) {
            dao = searchBoxDao;
        }

        @Override
        protected Void doInBackground(SearchBox... searchBoxes) {//既然传递了 searchBoxDao 进来，就是条件删除
            dao.deleteSearchBox(searchBoxes);
            return null;
        }
    }
    //delete 全部删除
    static class DeleteAllAsyncTask extends AsyncTask<Void,Void,Void>{

        private SearchBoxDao dao;

        public DeleteAllAsyncTask(SearchBoxDao searchBoxDao) {
            dao = searchBoxDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            dao.deleteAllSearchBox();
            return null;
        }
    }
    //query 全部查询
    static class QueryAsyncTask extends AsyncTask<Void,Void,Void>{

        private static final String TAG = MainActivity.class.getSimpleName();
        private SearchBoxDao dao;

        public QueryAsyncTask(SearchBoxDao searchBoxDao) {
            dao = searchBoxDao;
        }


        @Override
        protected Void doInBackground(Void... voids) {
            List<SearchBox> allSearchBox = dao.getAllSearchBox();

            //遍历全部查询结果
            for (SearchBox searchBox : allSearchBox) {
                Log.d(TAG, "doInBackground: 全部查询每一项" + searchBox.toString());
            }
            return null;
        }
    }
}
